using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Toenda.Rails.System;
using Toenda.Rails.DataContainer;

public partial class Controls_Main_ctrl_imagebar : System.Web.UI.UserControl {
	private AccountProvider ap = new AccountProvider();
	private ImageProvider ip = new ImageProvider();
	int startImg = 0;
	List<ImageItem> items = new List<ImageItem>();

	/// <summary>
	/// Page_Load
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e) {
		bool letView = false;
		bool isLoggedIn = false;
		bool isOwner = false;

		Account tmp = new Account();

		if(ip.CheckImageExists(Request.QueryString.Get("item"))) {
			//if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
			if(Session.Count > 0 && Session["ZapprUserId"] != null) {
				tmp = ap.GetAccount(Session["ZapprUserId"].ToString());
				isLoggedIn = true;
			}

			//plcLinkLeft.Controls.Clear();

			//Panel im = new Panel();
			//im.CssClass = "listSquareThumbnail imageBarLink";

			//plcLinkLeft.Controls.Add(im);

			ImageItem current = ip.GetImage(Convert.ToInt64(Request.QueryString.Get("item")));
			Account acc = ap.GetAccount(current.UserID);

			if(!string.IsNullOrEmpty(tmp.ID)) {
				if(acc.ID == tmp.ID) {
					isOwner = true;
				}
			}

			List<ImageItem> items = ip.GetImageList(
				acc.ID, 
				0, 
				9, 
				0, 
				true, 
				isOwner
			);

			int counter = 0;

			this.counter.Value = "9";

			foreach(ImageItem item in items) {
				// check privacy
				if(isLoggedIn && item.UserID == tmp.ID) {
					if(item.Access > 0) {
						// this time: only the user itself
						// next time: friends and family
						if(item.UserID == acc.ID) {
							letView = true;
						}
					}
					else {
						letView = true;
					}
				}
				else {
					if(item.Access == 0) {
						letView = true;
					}
				}

				if(letView) {
					if(counter < 9) {
						HyperLink img = new HyperLink();
						img.ImageUrl = "~/Images/" + item.Image75x75;
						img.CssClass = "listSquareThumbnail";
						//img.NavigateUrl = "~/default.aspx?item=" + item.ImageID.ToString();
						img.NavigateUrl = "~/photo/" + item.ImageID.ToString() + "/default.aspx";

						pImg.Controls.Add(img);

						counter++;
					}
					//else {
					//    int amount = ip.GetImageAmount(acc.ID);

					//    HyperLink imgn = new HyperLink();
					//    imgn.ImageUrl = "~/App_Themes/Default/Images/next_imagebar.jpg";
					//    imgn.CssClass = "listSquareThumbnail";
					//    //imgn.NavigateUrl = "~/default.aspx?item=" + item.ImageID.ToString();
					//    imgn.NavigateUrl = "~/photo/" + item.ImageID.ToString() + "/default.aspx";

					//    plcImages.Controls.Add(imgn);
					//}
				}

				letView = false;
			}
		}
	}

	/// <summary>
	/// btnLastImageList_Click
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnLastImageList_Click(object sender, EventArgs e) {
	}

	/// <summary>
	/// btnNewImageList_Click
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnNewImageList_Click(object sender, EventArgs e) {
		Account tmp = new Account();

		if(ip.CheckImageExists(Request.QueryString.Get("item"))) {
			//if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
			if(Session.Count > 0 && Session["ZapprUserId"] != null) {
				tmp = ap.GetAccount(Session["ZapprUserId"].ToString());
			}

			Account acc = ap.GetAccount(
				ip.GetImage(Convert.ToInt64(Request.QueryString.Get("item"))).UserID
			);

			this.startImg = Convert.ToInt32(this.counter.Value);

			this.items.Clear();
			this.items = ip.GetImageListForImageBar(
				acc.ID, 
				tmp,
				this.startImg,
				this.startImg + 9, 
				9
			);
			int counter = 0;

			this.startImg += 9;
			this.counter.Value = this.startImg.ToString();

			pImg.Controls.Clear();

			foreach(ImageItem item in items) {
				if(counter <= 8) {
					HyperLink img = new HyperLink();
					img.ImageUrl = "~/Images/" + item.Image75x75;
					img.CssClass = "listSquareThumbnail";
					//img.NavigateUrl = "~/default.aspx?item=" + item.ImageID.ToString();
					img.NavigateUrl = "~/photo/" + item.ImageID.ToString() + "/default.aspx";

					pImg.Controls.Add(img);

					counter++;
				}
			}
		}
	}
}
